#include "allpage.h"
#include <QPainter>
#include <QPixmap>
#include <mydata.h>

AllPage::AllPage(QWidget *parent) : QWidget(parent)
{

}

void AllPage::paintEvent(QPaintEvent *)
{
    QPainter painter;
    QBrush brush;
    brush.setStyle(Qt::SolidPattern);
    QPixmap pix(":/Image/RoadMap.jpg");

    QFont font("Times",16,QFont::Bold);
    QPen pen;

    painter.begin(&pix);
    painter.setFont(font);
    painter.setPen(pen);
    QPixmap carImg(":/Image/car.png");
    int roundCount = MyData::getObject()->roundCount();
    for(int i = 0;i<roundCount;i++)
    {
        Round * r =MyData::getObject()->getRound(i);
        brush.setColor(r->color);
        pen.setColor(r->color);
        painter.setBrush(brush);
        painter.setPen(pen);
        painter.drawRect(r->x,r->y,r->w,r->h);
    }
    int carCount = MyData::getObject()->carCount();
    for(int i=0;i<carCount;i++)
    {
        pen.setColor(Qt::black);
        painter.setPen(pen);
        Car *car = MyData::getObject()->getCar(i);
        painter.drawPixmap(car->x, car->y,30,30,carImg);
        painter.drawText(car->x+8,car->y+24,QString("%1").arg(car->id));
    }

    for(int i=0; i<MyData::getObject()->junctionCount(); i++)
    {
        for(int j = 0;j<MyData::getObject()->getJunction(i)->lampCount();j++)
        {
            SignalLamp *lamp = MyData::getObject()->getJunction(i)->getLamp(j);
            int roundIndex = lamp->roundIndex;
            QColor color = lamp->color;
            brush.setColor(color);
            pen.setColor(color);
            painter.setBrush(brush);
            painter.setPen(pen);
            Round *r =MyData::getObject()->getRound(roundIndex);
            int x,y,w,h;
            if(r->dir == 0)
            {
                //up
                w=33;
                h=5;
                x=r->x - 15;
                y=r->y - 6 -5;
            }
            else if (r->dir == 1) {
                //down
                w=33;
                h=5;
                x=r->x - 15;
                y=r->y +r->h +1 +5;
            }
            else if (r->dir ==2) {
                //left
                w=5;
                h=33;
                x=r->x-6 -5;
                y=r->y-15;
            }
            else {
                //right
                w=5;
                h=33;
                x=r->x+r->w+1+5;
                y=r->y-15;
            }
            painter.drawRect(x,y,w,h);
        }
    }

    painter.end();

    painter.begin(this);
    painter.drawPixmap(0,0,width(),height(),pix);
    painter.end();

}
